package com.colabo.j2ee.web.oaf.security.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.colabo.j2ee.web.oaf.menu.impl.MenuServiceImpl;
import com.colabo.j2ee.web.oaf.security.model.UserDetailsModel;
import com.colabo.j2ee.web.oaf.vo.IPResMenuVO;


public class MenuServiceAuthImpl extends MenuServiceImpl {
	public List doFilter(List menus) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
//		if (AuthUtil.isAdmin(auth)) {
//			return menus;
//		}
		
		UserDetailsModel model = (UserDetailsModel)auth.getPrincipal();
		int count = 0;
		while(!model.isAuth()){
			if(++count>10){
				break;
			}
	
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		if(model.isGod()){
			return menus;
		}
		
		List list = (List) model.getMenuAuthorities();
		
		if(null==list){
			return null;
		}
		
		List ret = new ArrayList();
		for (Iterator it = menus.iterator(); it.hasNext();) {
			IPResMenuVO menuVo = (IPResMenuVO) it.next();
			String id = menuVo.getMenuky();
			if(id==null){
				continue;
			}			
			
			if (list.contains(id)) {
				ret.add(menuVo);
			}
		}
		return ret;
	}
}
